﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2011
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package egallery.advanced.controllers.models.picture
{
    import egallery.advanced.display.DisplayList;
    import egallery.advanced.vo.PictureVO;
    
    import system.ioc.ObjectFactory;
    
    import vegas.controllers.AbstractController;
    import vegas.events.ModelObjectEvent;
    
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    
    /**
     * Invoked when the current picture in the picture model is changed.
     */
    public class ChangePicture extends AbstractController
    {
        /**
         * Creates a new ChangePicture instance.
         */
        public function ChangePicture() 
        {
            super();
        }
        
        /**
         * The IoC factory reference.
         */
        public var factory:ObjectFactory ;
        
        /**
         * The path of the pictures in the library.
         */
        public var path:String ;
        
        /**
         * The Google Analytics tracker.
         */
        public var tracker:* ;
        
        /**
         * Handles the event.
         */
        public override function handleEvent(e : Event):void 
        {
            var pic:PictureVO = ModelObjectEvent(e).getVO() as PictureVO ;
            var url:String    = ( path || "" ) + pic.url ;
            
            logger.debug( this + " handleEvent pic:" + pic + " url:" + url ) ;
            
            if ( factory == null )
            {
                logger.warn(this + " failed, the factory reference not must be null.") ;
                return ;
            }
            
            var loader:Loader = factory.getObject( DisplayList.PICTURE_LOADER ) as Loader ;
            if ( loader != null )
            {
                loader.load( new URLRequest( url ) ) ;
            }
            else
            {
                logger.warn(this + " failed, the loader reference not must be null.") ;
            }
            if ( tracker != null )
            {
                tracker.trackPageview( url ) ;
            }
        }
    }
}